package cn.csc.module.distribute.controller.admin.order;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.distribute.controller.admin.order.vo.*;
import cn.csc.module.distribute.controller.app.order.vo.AppDistributeOrderRespVO;
import cn.csc.module.distribute.convert.order.DistributeOrderConvert;
import cn.csc.module.distribute.dal.dataobject.order.DistributeOrderDO;
import cn.csc.module.distribute.service.order.DistributeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Slf4j
@Api(tags = "管理后台 - 分销订单记录")
@RestController
@RequestMapping("/distribute/order")
public class DistributeOrderController {

    @Resource
    private DistributeOrderService orderService;

    @PostMapping("/create")
    @ApiOperation("创建分销订单记录")
    @PreAuthorize("@ss.hasPermission('distribute:order:create')")
    public CommonResult<BigInteger> createOrder(@Valid @RequestBody DistributeOrderCreateReqVO createReqVO) {
        return success(orderService.createOrder(createReqVO));
    }

    /**
     * 更新分销订单记录
     * @param
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("更新分销订单记录")
    @PreAuthorize("@ss.hasPermission('distribute:order:update')")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody DistributeOrderUpdateReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除分销订单记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:order:delete')")
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") BigInteger id) {
        orderService.deleteOrder(id);
        return success(true);
    }

    /**
     * 获得分销订单记录
     * @param
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("获得分销订单记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:order:query')")
    public CommonResult<DistributeOrderRespVO> getOrder(@RequestParam("id") BigInteger id) {
        DistributeOrderDO order = orderService.getOrder(id);
        return success(DistributeOrderConvert.INSTANCE.convert(order));
    }

    @GetMapping("/list")
    @ApiOperation("获得分销订单记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('distribute:order:query')")
    public CommonResult<List<DistributeOrderRespVO>> getOrderList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<DistributeOrderDO> list = orderService.getOrderList(ids);
        return success(DistributeOrderConvert.INSTANCE.convertList(list));
    }

    /**
     * 获得分销订单记录分页
     * @param
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("获得分销订单记录分页")
    @PreAuthorize("@ss.hasPermission('distribute:order:query')")
    public CommonResult<PageResult<AppDistributeOrderRespVO>> getOrderPage( DistributeOrderPageReqVO pageVO) {
        log.info("[DistributeOrderServiceImpl][getOrderPage][DistributeOrderPageReqVO]{}",pageVO);

        PageResult<AppDistributeOrderRespVO> pageResult = orderService.selectAdminOrderPage(pageVO);
        return success(pageResult);
    }

    /**
     * 导出分销订单记录
     * @param
     * @return
     */
    @GetMapping("/export-excel")
    @ApiOperation("导出分销订单记录 Excel")
    @PreAuthorize("@ss.hasPermission('distribute:order:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderExcel(DistributeOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        List<DistributeOrderExcelVO> list = orderService.getOrderExportList(pageReqVO);

        ExcelUtils.write(response, "分销订单记录.xls", "数据", DistributeOrderExcelVO.class, list);
    }

}
